#ifndef __HOW2GOQUERY__
#define __HOW2GOQUERY__

#include "HTTPUtils.h"
#include <euserhl.h>
#include "ActiveTarget.h"
#include <lbsposition.h>
#include <http.h>
#include <es_sock.h>
#include <Function.h>
#include "JourneyPlanner.h"


class CHow2GoQuery : public CBase
	{

public:
	
	struct COutput : public CBase
		{
		HBufC8 *iHow2GoAnswer;
		TPtrC8 iHow2GoStatus, iHow2GoReason;
		struct CItinerary
			{
			struct TItem
				{
				enum { ELEG_FROM, ELEG_TO, ETOTAL } iType;
				TPtrC8 iBuf;
				};
			RArray< TItem > iItems;
			~CItinerary();
			};
		RArray< CItinerary* > iItineraries;
		COutput();
		~COutput();
		};
	

	
	typedef scalp::CFunction< void(CHow2GoQuery*, TInt, TInt, CJourneyPlannerOutput*) > TCallbackType;
	typedef scalp::CFunction< void(CHow2GoQuery*, TInt, TInt, COutput*) > TCallbackType2;
	
	LManagedPtr<TCallbackType> iCallback;
	LManagedPtr<TCallbackType2> iCallback2;
	LManagedPtr<CDeflateDecompressor> iDeflateDecompressor;
	
	RHTTPSession iHTTPSession;
	void SetConnectionInfoL(const RConnection& aConnection, const RSocketServ& aSocketServ);
	void CloseConnectionL();
	void SubmitL(const TCoordinate& aOrigin, const TCoordinate& aDestination, TInt aTravelOption, CHow2GoQuery::TCallbackType* aCallback);
	void Submit2L(const TCoordinate& aOrigin, const TCoordinate& aDestination, TInt aTravelOption, CHow2GoQuery::TCallbackType* aCallback);
	
	// constructors and destructor
	CHow2GoQuery();
	static CHow2GoQuery* NewL();
	static CHow2GoQuery* NewLC();        
	void ConstructL();
	virtual ~CHow2GoQuery();
							
	RTimer iHow2GoTimeoutTimer;
	LManagedPtr<scalp::CActiveTarget> iHow2GoTimeoutTarget;
	LManagedPtr<CHTTPTransactionSource> iHow2GoTransactionSource;
	LManagedPtr<CHTTPTransactionTarget> iHow2GoTransactionTarget;
	RHTTPTransaction iHow2GoTransaction;
	TCoordinate iOrigin, iDestination;
	TInt iTravelOption;
	TInt iHow2GoStatusCode;

	void How2GoTransactionTimeoutL();
	
	void Cancel();
	
	TBool Pending() const;

	TInt How2GoTransactionError(TInt aError, RHTTPTransaction aTransaction);
	void ReceivedHow2GoAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedHow2GoAnswer2L(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	
	};

#endif // __HOW2GOQUERY__
